import os
import subprocess
import sys

def build_exe():
    """构建exe文件"""
    print("开始构建exe文件...")
    
    # PyInstaller命令
    cmd = [
        "pyinstaller",
        "--onefile",  # 打包成单个exe文件
        "--windowed",  # 不显示控制台窗口
        "--name=ImageSelector",  # 指定exe文件名
        "--add-data=requirements.txt;.",  # 包含依赖文件
        "--hidden-import=cv2",
        "--hidden-import=numpy",
        "--hidden-import=PIL",
        "--hidden-import=tkinter",
        "--hidden-import=pathlib",
        "--hidden-import=logging",
        "--hidden-import=threading",
        "--hidden-import=hashlib",
        "--hidden-import=collections",
        "--hidden-import=shutil",
        "--hidden-import=os",
        # 设置编码为UTF-8，避免中文乱码
        "--distpath=dist",
        "--workpath=build",
        "--specpath=.",
        "image_selector.py"
    ]
    
    try:
        # 执行PyInstaller命令
        result = subprocess.run(cmd, check=True, capture_output=True, text=True, encoding='utf-8')
        print("构建成功！")
        print("exe文件位置: dist/ImageSelector.exe")
        return True
    except subprocess.CalledProcessError as e:
        print(f"构建失败: {e}")
        print(f"错误输出: {e.stderr}")
        return False
    except FileNotFoundError:
        print("错误: 未找到PyInstaller，请先安装: pip install pyinstaller")
        return False

if __name__ == "__main__":
    build_exe() 